﻿using HCQ2_Model;
using HCQ2_Model.SchoolClass;
using HCQ2_Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using HCQ2_Common;

namespace HCQ2UI_Logic.ExperienceController
{
    /// <summary>
    /// 班级
    /// </summary>
    public class SchoolClassController:BaseLogic
    {
        /// <summary>
        /// 所属数据字典父ID
        /// </summary>
        private const int UNIT_ITEM_ID = 8;

        public ActionResult Index()
        {
            //所属学校列表
            var unitLists = operateContext.bllSession.T_ItemCodeMenum.ListByItemId(UNIT_ITEM_ID);

            ViewBag.units = unitLists;
            return View();
        }

        /// <summary>
        /// 新增或修改
        /// </summary>
        /// <param name="id">为空新增，不为空修改</param>
        /// <returns></returns>
        public ActionResult dialog(Nullable<int> id)
        {
            //所属单位列表
            var unitLists = operateContext.bllSession.T_ItemCodeMenum.ListByItemId(UNIT_ITEM_ID);
            ViewBag.units = unitLists;
           
            if (id != null) {
                ViewBag.id = id;
            }

            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult list(SchoolClassListQuery query)
        {
            query.currentUserId = operateContext.Usr.user_id;
            TableModel tableModel = operateContext.bllSession.T_Class.GetClassList(query);

            return operateContext.ok(tableModel);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult save(SchoolClassEdit data)
        {
            data.updateUser = operateContext.Usr.user_name;
            int id = operateContext.bllSession.T_Class.SaveClass(data);
            return operateContext.ok(id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult del(int id)
        {
            string mess = operateContext.bllSession.T_Class.DelClass(id);
            if (string.IsNullOrEmpty(mess))
            {
                return operateContext.ok();
            }
            return operateContext.error(mess);
        }
        /// <summary>
        /// 通过ID获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult find(Nullable<int> id)
        {
            T_Class data = operateContext.bllSession.T_Class.FindSingle(w=>w.id == id.Value);
            var teachers = operateContext.bllSession.T_Class.ListTeacherSelect(data.schoolName);

            SchoolClassFindData findData = data.MapTo<SchoolClassFindData>();
            findData.entranceYear = findData.entranceYear == 0 ? null : findData.entranceYear;
            findData.classStudentNum = findData.classStudentNum == 0 ? null : findData.classStudentNum;
            findData.teacherSelectList = teachers;

            return operateContext.ok(findData);
        }

        /// <summary>
        /// 获取班主任列表
        /// </summary>
        /// <param name="unit">学校</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult getTeacher(string unit)
        {
            var teachers = operateContext.bllSession.T_Class.ListTeacherSelect(unit);
            return operateContext.ok(teachers);
        }
    }
}
